package Connection;

import javafx.scene.control.Alert;
import javafx.stage.StageStyle;
import java.sql.*;

public class MyConnection {
    private Connection connection = null;
    private Statement statement = null;
    private final String dburl = "jdbc:mysql://localhost:3306/bookmanager"; //驱动
    private String Name = "";
    private String Password = "";


    public MyConnection(String Name, String Password){
        this.Connection(Name, Password);
    }

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    public Connection getConnection() {
        return this.connection;
    }
    public Statement getStatement() {
        return this.statement;
    }
    public void setStatement(Statement statement) {
        this.statement = statement;
    }

    public String getName() {
        return this.Name;
    }

    public void setName(String name) {
        this.Name = name;
    }

    public String getPassword() {
        return this.Password;
    }

    public void setPassword(String password) {
        this.Password = password;
    }

    /*
     *Login -> 登录录入账号/密码
     */
    public void Login(String name, String password){
        setName(name);
        setPassword(password);
    }

    /*
     * Connection -> 连接数据库
     * 注册数据表属性:ISBN号(主键), 书名, 作者, 出版社, 库存量, 价格, 位置, 类别, 备注
     * 构建账户账号/密码
     */
    public void Connection(String Name, String Password){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Login(Name, Password);
            setConnection(DriverManager.getConnection(dburl, getName(), getPassword()));
            setStatement(getConnection().createStatement());
        } catch (SQLException se) {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Error");
            alert.setHeaderText("触发错误");
            alert.setContentText("数据库连接账号/密码错误");
            alert.initStyle(StageStyle.UTILITY);//不显示图标
            alert.show();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

